package com.jz.partclass.service.impl;

import com.jz.legalpersonclass.model.LegalPersonVo;
import com.jz.legalplantclass.entity.LegalPlantClassAddVo;
import com.jz.legalplantclass.entity.LegalVO;
import com.jz.legalplantclass.entity.Legalplantclass;
import com.jz.legalplantclass.mapper.LegalplantclassMapper;
import com.jz.partclass.mapper.PartMapper;
import com.jz.partclass.model.*;
import com.jz.partclass.service.PartService;
import com.jz.planclass.model.PlanClassVo;
import com.jz.subclass.model.SubClassVo;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class PartServiceImpl implements PartService {

    @Autowired
    private PartMapper partMapper;

    @Override
    public ResultVo firstPage(PartSelectVo partSelectVo) {
        List<PartVo> partVoList = partMapper.queryData(partSelectVo);
        //查询数量
        for (PartVo partVo : partVoList) {

            int status = partVo.getStatus() ? 1 : 0;
            partVo.setStatusName(SubStatusEnum.getNameByStatus(status));
        }
        int count = partMapper.queryCount(partSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(partVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(partSelectVo.getPageIndex());
        allRecordsVo.setPageSize(partSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addpartClass(PartAddVo partAddVo) {
        //验证编码是否存在
        PartVo partVO = partMapper.queryByCode(partAddVo.getCode());
        if (partVO != null) {
            return ResultBuildVo.error("备件编码已经存在！");
        }
        //验证名称是否存在
        PartVo subClassName = partMapper.queryByName(partAddVo.getName());
        if (subClassName != null) {
            return ResultBuildVo.error("备件名称已经存在！");
        }
        Date nowDate = new Date();
        PartVo classVo = new PartVo();
        BeanUtils.copyProperties(partAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        partMapper.add(classVo);
        partMapper.add1(classVo);
        return ResultBuildVo.success();
    }

//
//    @Override
//    public ResultVo queryById(Integer id) {
//
//        CatVo partVo = partMapper.queryById(id);
//
//        return ResultBuildVo.success(partVo);
//    }
//
    @Override
    public ResultVo updateSubClass(@Valid PartUpdateVo partUpdateVo) {
        List<PartVo> partVo = partMapper.queryById(partUpdateVo.getId());
        if (partVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }

//        if (partVo.getStatus()) {
//            return ResultBuildVo.error("只有禁用状态才能修改");
//
//        }
        PartVo subClassName = partMapper.queryByName(partUpdateVo.getName());
        if (subClassName != null && partUpdateVo.getId() != subClassName.getId()) {

            return ResultBuildVo.error("修改名称已存在");
        }

        Date nowDate = new Date();
        PartVo classVo = new PartVo();
        BeanUtils.copyProperties(partUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
         partMapper.update(classVo);
         partMapper.delete(classVo.getCode());
         partMapper.insertPartLegal(partUpdateVo);

//        if (version == 0){
//
//            return ResultBuildVo.error("已被修改，刷新重试");
//        }
        return ResultBuildVo.success();

    }

    @Override
    public ResultVo queryByUserId(Integer userId) {

       List<PartVo> legalPersonVoLists = partMapper.queryByUserId(userId);

        return  ResultBuildVo.success(legalPersonVoLists);
    }

    @Override
    public ResultVo queryAll() {

       List<SubClassVo> subClassVoList =  partMapper.queryAll();

       return ResultBuildVo.success(subClassVoList);
    }

    @Override
    public ResultVo queryAll2() {

      List<PlanClassVo> planClassVoList =   partMapper.queryAll2();
        return ResultBuildVo.success(planClassVoList);
    }

//
    @Override
    public ResultVo updateStatus(@Valid PartUpdateStatusVo partUpdateStatusVo) {
        List<PartVo> partVo = partMapper.queryById(partUpdateStatusVo.getId());
        if (partVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        PartVo classVo = new PartVo();
        BeanUtils.copyProperties(partUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        partMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }

}

